<?php
class ServiceController extends BackendController{

    public $active = "plugins-service";
    public $plug_version = "1.0";
    public $plug_title = "首页服务";
    public $plug_description = "网站首页的服务功能列表展示";
    public $plug_controller = "service";

    public function actionIndex(){

        $action_type = isset($_REQUEST['action_type'])?$_REQUEST['action_type']:null;

        //  plug_service_id, icon_cls, title, description, seq

        if($action_type == 'insert' || $action_type =='updatedo'){
            $plug_service_id = $_REQUEST['plug_service_id'];
            $icon_cls = $_REQUEST['icon_cls'];
            $title = $_REQUEST['title'];
            $description = $_REQUEST['description'];
            $seq = $_REQUEST['seq'];

            if($plug_service_id == 0){
                $this->connection->createCommand("insert into rk_plug_service (icon_cls, title, description, seq) values
 (:icon_cls, :title, :description, :seq)")
                    ->bindParam("icon_cls",$icon_cls)
                    ->bindParam("title",$title)
                    ->bindParam("description",$description)
                    ->bindParam("seq",$seq)
                    ->execute();
            }else{
                $this->connection->createCommand("update rk_plug_service set icon_cls = :icon_cls,title =:title,description=:description,seq=:seq 
where plug_service_id = :plug_service_id ")
                    ->bindParam("icon_cls",$icon_cls)
                    ->bindParam("title",$title)
                    ->bindParam("description",$description)
                    ->bindParam("seq",$seq)
                    ->bindParam("plug_service_id",$plug_service_id)
                    ->execute();
            }

        }else if($action_type == 'update'){
            $plug_service_id = $_REQUEST['plug_service_id'];
            $this->data['update_service'] = $this->connection->createCommand("select * from rk_plug_service where plug_service_id = :plug_service_id ")
                ->bindParam("plug_service_id",$plug_service_id)
                ->queryRow();
        }else if($action_type == 'delete'){
            $plug_service_id = $_REQUEST['plug_service_id'];
            $this->connection->createCommand("delete from rk_plug_service where plug_service_id = :plug_service_id ")
                ->bindParam("plug_service_id",$plug_service_id)
                ->execute();
        }

        $this->data['services'] = $this->connection->createCommand("select * from rk_plug_service order by seq desc")->queryAll();

        $this->render("index",$this->data);
    }

    public function actionInstall(){

        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_service`")->execute();
        $this->connection->createCommand("CREATE TABLE `rk_plug_service` (
  `plug_service_id` int(11) NOT NULL AUTO_INCREMENT,
  `icon_cls` varchar(255) DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  `seq` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`plug_service_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8")->execute();
        $this->connection->createCommand("insert into rk_plug (plug_controller,plug_version,plug_key) values (:controller,:version,:key)")
            ->bindParam(":controller",$this->plug_controller)
            ->bindParam(":version",$this->plug_version)
            ->bindParam(":key",$this->active)
            ->execute();
        $parentMenuId = $this->connection->createCommand("SELECT menu_id FROM rk_menu WHERE menu_key = 'plugins'")->queryScalar();
        $this->connection->createCommand("INSERT INTO rk_menu ( menu_text, parent_id, url, menu_key ) VALUES (:menu_text,:parent_id,'index.php?r=plugins/service',:menu_key)")
            ->bindParam(":menu_text",$this->plug_title)
            ->bindParam(":parent_id",$parentMenuId)
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }

    public function actionUninstall(){
        $this->connection->createCommand("delete from rk_plug where plug_key = :plug_key")
            ->bindParam(":plug_key",$this->active)
            ->execute();
        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_service`")->execute();
        $this->connection->createCommand("delete from rk_menu where menu_key = :menu_key")
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }
}